package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.constants.HotArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

@Service
@Transactional
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements ApLikesBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    KafkaTemplate kafkaTemplate;
    /**
     * 存储喜欢数据
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        if (dto==null || dto.getArticleId()==null
        ||dto.getType()<0||dto.getType()>2
        ||dto.getOperation()<0||dto.getOperation()>1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService
                .findByUserIdOrEquipmentId(user.getId(), null);
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApLikesBehavior apLikesBehavior = getOne(
                Wrappers.<ApLikesBehavior>lambdaQuery()
                        .eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId())
                        .eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                        .eq(ApLikesBehavior::getType,dto.getType())
        );
        //点赞表无数据，但是入参是取消点赞
        if (apLikesBehavior==null
        && dto.getOperation().equals(ApLikesBehavior.Operation.CANCEL.getCode())){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"暂无点赞");
        }
        //点赞表有数据，但入参是点赞
        if (apLikesBehavior!=null
        && dto.getOperation().equals(ApLikesBehavior.Operation.LIKE.getCode())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"无需重复点赞");
        }

        //*****添加文章点赞-发送消息******
        if(dto.getOperation() ==0){
            UpdateArticleMess mess = new UpdateArticleMess();
            mess.setAdd(3);
            mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
            mess.setArticleId(dto.getArticleId());
            kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,
                    UUID.randomUUID().toString(), JSON.toJSONString(mess));
        }
        //**********************************

        //保存点赞 点赞行为表中插入数据
        if (apLikesBehavior==null
                && dto.getOperation().equals(ApLikesBehavior.Operation.LIKE.getCode())){
            apLikesBehavior=ApLikesBehavior.builder()
                    .operation(dto.getOperation())
                    .createdTime(new Date())
                    .type(dto.getType())
                    .articleId(dto.getArticleId())
                    .entryId(apBehaviorEntry.getId())
                    .build();
            save(apLikesBehavior);
            return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
        }
        //取消点赞  更新点赞行为的状态
        apLikesBehavior.setOperation(dto.getOperation());
        updateById(apLikesBehavior);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }
}
